package com.example.work12.service;

import android.app.Application;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.RemoteViews;

import com.example.work12.R;
import com.example.work12.application.MyApp;
import com.example.work12.bean.Music;
import com.example.work12.utils.MusicUtils;

import java.io.IOException;
import java.util.List;

public class MyService extends Service {
    private MediaPlayer mediaPlayer;
    private List<Music> musicList;
    private MyApp application;
    @Override
    public void onCreate() {
        super.onCreate();
        application = (MyApp) getApplication();
        mediaPlayer = new MediaPlayer();
        musicList = MusicUtils.getMusic(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("标题");

        RemoteViews views = new RemoteViews(getPackageName(), R.layout.no_layout);
        Intent intent1 = new Intent("shang");
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.shang,broadcast);

        Intent intent2 = new Intent("next");
        PendingIntent broadcast1 = PendingIntent.getBroadcast(this, 2, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.next,broadcast1);

        Intent intent3 = new Intent("stop");
        PendingIntent broadcast2 = PendingIntent.getBroadcast(this, 3, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.stop,broadcast2);

        Intent intent4 = new Intent("start");
        PendingIntent broadcast4 = PendingIntent.getBroadcast(this, 4, intent4, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.start,broadcast4);

        builder.setCustomContentView(views);

        startForeground(1,builder.build());
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    public class MyBinder extends Binder{
        public MyService getInstance(){
            return MyService.this;
        }
    }

    public void callPlay(int i){
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(musicList.get(i).getData());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stop(){
        if(mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }
    }

    public void shang(){
        if(--application.index <= 0){
            application.index = 0;
        }
        callPlay(application.index);
    }

    public void next(){
        if(++application.index >= musicList.size() -1){
            application.index = 0;
        }
        callPlay(application.index);
    }

    public void start(){
        mediaPlayer.start();
    }
}
